#coding=utf-8
'''
Created on Dec 10, 2015
@author: DQA wangpeng 
'''
import data.desired_setting

import data.logging_save

import time
from appium import webdriver
import data.config
import data.get_status
import threading
from appium.webdriver.common.touch_action import TouchAction
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

import os
import sys
from time import ctime
from cookielib import logger
reload(sys)
sys.setdefaultencoding('utf-8')

import data.logging_save
import data.Create_Log_Folder
import unittest

class test(unittest.TestCase):
    def setUp(self):
        #startup the huawei m130 app
        self.save_log = data.Create_Log_Folder.Create('xiami_feeling')
        desired_caps = data.desired_setting.desired_setting()
        self.driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)
        data.logging_save.logging.info("startup the app already")
        print 'startup the app already'
        
    def tearDown(self):
        self.driver.quit()
        
    def test_alert(self):
        u"""测试步骤：点击“虾米”音乐首页的“心情”模块，然后点击进去，轮播里面的当页歌曲，再按“后退”返回按键，循环播放       测试依据：底部获取到的名字和playback界面获取名字一致"""
        #excute the test case times
        playback_times = 1
        success_times = 1
        fail_times = 1
        savenumber = 1
        save_pic = os.path.join(self.save_log+str(savenumber)+'.png')
        print save_pic
        action = TouchAction(self.driver)
        while(playback_times<=2):
            alert_number = 1
            while(alert_number <=10):
                try:
                    WebDriverWait(self.driver, 10).until(EC.presence_of_element_located((By.XPATH,data.config.alert_popup())))     
                    alert = self.driver.switch_to_alert()
                    alert.accept()
                    alert_number = alert_number +1
                    
                    if alert_number == 10:
                        data.logging_save.logging.info("网络环境太差，一直弹出来“网络错误提示，”无法继续测试")
                        print time.ctime(),"网络环境太差，一直弹出来“网络错误提示，”无法继续测试"
                        self.driver.get_screenshot_as_file(save_pic)
                        savenumber = savenumber + 1
                        self.fail("the network envoiroment is too bad")
                        
                except:
                    time.sleep(10)
                    el_style = self.driver.find_element_by_xpath(data.config.Get_xiami_feeling())
                    time.sleep(3)
                    action.tap(el_style).perform()
                    #self.driver.find_element_by_xpath(data.config.Get_xiami_commend()).click()
                    data.logging_save.logging.info("点击进入“心情”界面，进入到播放界面")
                    print time.ctime(),"点击进入“心情”界面，进入到播放界面"
                    alert_number = 11
                
            
            while(alert_number <=15):        
                try:
                    WebDriverWait(self.driver, 10).until(EC.presence_of_element_located((By.XPATH,data.config.alert_popup())))     
                    alert = self.driver.switch_to_alert()
                    alert.accept()
                    alert_number = alert_number +1
                    if alert_number == 10:
                        data.logging_save.logging.info("网络环境太差，一直弹出来“网络错误提示，”无法继续测试")
                        print time.ctime(),"网络环境太差，一直弹出来“网络错误提示，”无法继续测试"
                        self.driver.get_screenshot_as_file(save_pic)
                        savenumber = savenumber + 1
                        self.fail("the network envoiroment is too bad")
                        
                except:
                    time.sleep(10)
                    #WebDriverWait(self.driver, 10).until(EC.presence_of_element_located((By.XPATH,data.config.Xiami_first_song_name())))    
                    els_list = self.driver.find_elements_by_class_name("UIATableCell")
                    song_length = len(els_list)
                    if song_length >=6:
                        for i in range(6):
                            time.sleep(10)
                            test = os.path.join(data.config.Get_xiami_speicail_xpath_tuijian()+str(i+1)+"]/UIAStaticText[1]")
                            el_play = self.driver.find_element_by_xpath(test)
                            action.tap(el_play).perform()
                            time.sleep(10)
                            playback_list_name = self.driver.find_element_by_xpath("//UIAApplication[1]/UIAWindow[2]/UIAScrollView[1]/UIAStaticText[2]").text
                            #print data.config.playback_button_songname_next_page()
                            button_name_length = len(playback_list_name)
                            for before_name in range(button_name_length):
                                if playback_list_name[before_name] == '-':
                                    break
                            print "播放器底部获取到的歌曲名字是",playback_list_name[0:before_name].encode('utf-8')
                            data.logging_save.logging.info("播放器底部获取到的歌曲名字是"+str(playback_list_name[0:before_name].encode('utf-8'))) 
                            
                            time.sleep(5)
                            #entry into the playback menu
                            self.driver.tap([(40,500)])
                            
                            time.sleep(2)
                            get_songname_playback_page = self.driver.find_element_by_xpath(data.config.playback_page_songname()).text
                            time.sleep(5)
                            data.logging_save.logging.info("通过网络获取到的歌曲名字是:"+str(get_songname_playback_page))
                            print time.ctime(),"通过网络获取到的歌曲名字是：%s"%get_songname_playback_page
                            time.sleep(2)
                            #loop the list playback
                            try:
                                self.assertIn(playback_list_name[0:before_name],get_songname_playback_page) 
                                #self.assertIn(get_songname_http,playback_list_name) 
                                print ctime(),"经过对比，文件名比对一致：成功次数为 %d 次"%success_times
                                data.logging_save.logging.info("经过对比，文件名比对一致：成功次数为"+str(success_times)+"次")
                                success_times = success_times + 1
                                time.sleep(4)
                                #self.driver.find_element_by_xpath(data.config.back_previous_menu()).click()
                                #print 'back to the menu'
                                data.logging_save.logging.info("contiune to test")
                                time.sleep(5)
                                alert_number = 16
                                #back to the previous page
                                time.sleep(5)
                                el_back = self.driver.find_element_by_name("back normal")
                                data.logging_save.logging.info("back to previous play list")
                                action.tap(el_back).perform()
                                
                            except:
                                data.logging_save.logging.warning("底部获取名字和实际获取名字不一致")
                                print time.ctime(),"底部获取名字和实际获取名字不一致"
                                self.driver.get_screenshot_as_file(save_pic)
                                savenumber = savenumber + 1
                                self.fail("the current song name do not match the real songname after playback")
                    
                    else:
                        for i in range(song_length):
                            time.sleep(10)
                            test = os.path.join(data.config.Get_xiami_speicail_xpath_tuijian()+str(i+1)+"]/UIAStaticText[1]")
                            el_play = self.driver.find_element_by_xpath(test)
                            action.tap(el_play).perform()
                            time.sleep(10)
                            playback_list_name = self.driver.find_element_by_xpath("//UIAApplication[1]/UIAWindow[2]/UIAScrollView[1]/UIAStaticText[2]").text
                            #print data.config.playback_button_songname_next_page()
                            button_name_length = len(playback_list_name)
                            for before_name in range(button_name_length):
                                if playback_list_name[before_name] == '-':
                                    break
                            print playback_list_name[0:before_name].encode('utf-8')
                            time.sleep(5)
                            #entry the playback menu
                            self.driver.tap([(40,500)])
                            
                            time.sleep(2)
                            get_songname_playback_page = self.driver.find_element_by_xpath(data.config.playback_page_songname()).text
    
                            time.sleep(5)
                            data.logging_save.logging.info("通过网络获取到的歌曲名字是:"+str(get_songname_playback_page))
                            print time.ctime(),"通过网络获取到的歌曲名字是：%s"%get_songname_playback_page
                            time.sleep(2)
        
                            try:
                                print playback_list_name[0:before_name]
                                print get_songname_playback_page
                                self.assertIn(playback_list_name[0:before_name],get_songname_playback_page) 
                                #self.assertIn(get_songname_http,playback_list_name) 
                                print ctime(),"经过对比，文件名比对一致：成功次数为 %d 次"%success_times
                                data.logging_save.logging.info("经过对比，文件名比对一致：成功次数为"+str(success_times)+"次")
                                success_times = success_times + 1
                                time.sleep(4)
                                #self.driver.find_element_by_xpath(data.config.back_previous_menu()).click()
                                #print 'back to the menu'
                                data.logging_save.logging.info("contiune to test")
                                time.sleep(5)
                                alert_number = 16
                                
                                
                                #back to the previous page
                                time.sleep(5)
                                
                                el_back = self.driver.find_element_by_name("back normal")
                                data.logging_save.logging.info("back to previous play list")
                                action.tap(el_back).perform()
                                
                            except:
                                data.logging_save.logging.warning("底部获取名字和实际获取名字不一致")
                                print time.ctime(),"底部获取名字和实际获取名字不一致"
                                self.driver.get_screenshot_as_file(save_pic)
                                savenumber = savenumber + 1
                                self.fail("the current song name do not match the real songname after playback")
            
                               
            playback_times = playback_times + 1
            time.sleep(5)
            el_back = self.driver.find_element_by_name("back normal")
            data.logging_save.logging.info("back to previous play list,test it again")
            action.tap(el_back).perform()
            print 'back to the main menu'
        
        print 'finished the test'    
if __name__ == '__main__':
    unittest.main()
    print 'finished'